<?php

namespace IMT\UserBundle\Form\Handler;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;


class RegistrationFormHandler extends BaseHandler
{

    public function process($confirmation = false)
    {
        $user = $this->userManager->createUser();
        $user->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36));
        $user->setEnabled(false);
        $user->setLocked(false);
        $user->setExpired(false);
        $user->setRoles(array("ROLE_USER"));
        $user->setCredentialsExpired(false);
        
        $this->form->setData($user);

        if ('POST' == $this->request->getMethod()) {
            $this->form->bindRequest($this->request);

            if ($this->form->isValid()) {
                $this->onSuccess($user, $confirmation);

                return true;
            }
        }

        return false;
    }

    protected function onSuccess(UserInterface $user, $confirmation)
    {
        // Attribution du participant au user
        //$participant = $user->getParticipant();
        //$participant->setUser($user);
        //$user->setParticipant($participant);
    
        parent::onSuccess($user, $confirmation);
	}
}

